package com.ycdx.zwzc.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.ycdx.zwzc.impl.UserImpl;
import com.ycdx.zwzc.pojo.User;
import com.ycdx.zwzc.utils.MD5Util;

public class UserAction extends ActionSupport implements Preparable{
	
	private List<User> allUsers;
	private static final long serialVersionUID = -512728776868877795L;
	private String id;
	private String newPassword;
	public String getNewPassword() {
		return newPassword;
	}
	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}


	private User user;
	
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public List<User> getAllUsers() {
		return allUsers;
	}
	public void setAllUsers(List<User> allUsers) {
		this.allUsers = allUsers;
	}
	public String show(){
		return "success";
	}
	public String add(){
		return "success";
	}
	/**
	 * 添加人员
	 * @return
	 */
	public String addNew(){
		//对原密码进行加密
		user.setPassword(MD5Util.MD5(user.getPassword()));
		int flag = UserImpl.addUser(user);
		//System.out.println(request.getEditor());
		if(flag==1){
			//添加成功
			addActionMessage("人员添加成功！");
			
		}else{
			//添加不成功
			addActionMessage("人员添加不成功，请重试！");
			
		}
		return "list";
	}
	
	public String edit(){
		//System.out.println(id);
		user = UserImpl.getUserById(id);
		return "success";
	}
	
	/**
	 * 人员信息修改
	 * @return
	 */
	public String update(){
		//判断是否修改了密码
		if(newPassword==null||newPassword.equals("")){
			//未修改密码
			int flag = UserImpl.update(user);
			//System.out.println(request.getEditor());
			if(flag==1){
				//添加成功
				addActionMessage("人员修改成功！");
				
			}else{
				//添加不成功
				addActionMessage("人员修改不成功，请重试！");
				
			}
		}else{
			//修改了密码
			user.setPassword(MD5Util.MD5(newPassword));
			
			int flag = UserImpl.updateWithPassword(user);
			//System.out.println(request.getEditor());
			if(flag==1){
				//添加成功
				addActionMessage("人员修改成功！");
				
			}else{
				//添加不成功
				addActionMessage("人员修改不成功，请重试！");
				
			}
		}
		
		return "list";
	}
	
	public String delete(){
		//System.out.println(id);
		int flag = UserImpl.deleteUser(id);
		//System.out.println(flag);
		if(flag==1){
			//删除成功
			addActionMessage("人员删除成功！");
			
		}else{
			//添加不成功
			addActionMessage("人员删除不成功，请重试！");
			
		}
		return "list";
	}
	
	
	/**
	 * 初始化产品类型
	 */
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		allUsers = UserImpl.getAllCommonUser();
	}
}
